package com.example.springboot4.system.model;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.List;
import java.util.stream.Stream;

/**
 * 参数接受
 *
 * @author 宋义州
 * @since 2024/1/16 9:24
 */
@SuppressWarnings(value = {"unused", "unchecked"})
public class Params extends JSONObject {

    public Params() {
    }

    public <T> List<T> getList(String key, Class<T> c) {
        return this.getJSONArray(key).toJavaList(c);
    }

    public <T> T[] getArray(String key, Class<T> c) {
        if (this.containsKey(key)) {
            List<T> arr = this.getList(key, c);
            Stream<T> var10000 = arr.stream();
            return (T[]) var10000.toArray();
        } else {
            return null;
        }
    }

    public Object[] getArray(String key) {
        if (this.containsKey(key)) {
            JSONArray arr = this.getJSONArray(key);
            if (arr != null) {
                return this.getJSONArray(key).toArray();
            }
        }

        return null;
    }

    public static Params builder(String key, Object value) {
        Params params = new Params();
        params.put(key, value);
        return params;
    }

    public static Params toParam(Object o) {
        return JSON.parseObject(JSON.toJSONString(o), Params.class);
    }

    public Params toParam() {
        return JSON.to(Params.class, this);
    }

    public boolean isEmptyValue(String key) {
        if (this.containsKey(key)) {
            return this.get(key) == null;
        } else {
            return true;
        }
    }

    public void require(String... keys) {
        for (String k : keys) {
            if (!this.containsKey(k)) {
                throw new RuntimeException("缺少关键参数");
            }
        }
    }

}
